package dead_lock;

public class Test2 {
    public static void main(String[] args) {
        Object A = new Object();
        Object B = new Object();
        Thread t1 = new Thread(()->{
            synchronized (A) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                // 尝试获取 B, 但没有释放 A
                synchronized (B) {
                    System.out.println("线程 1 拿到了两把锁");
                }
            }
        });

        Thread t2 = new Thread(()->{
            synchronized (A) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                // 尝试获取到 B, 并没有释放 A
                synchronized (B) {
                    System.out.println("线程 2 拿到了两把锁");
                }
            }
        });

        t1.start();
        t2.start();
    }
}
